package org.jeecg.modules.product.risk.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.product.risk.entity.TbBaseCheckDanger;
import org.jeecg.modules.product.risk.entity.TbBaseDangerInvestigation;
import org.jeecg.modules.product.risk.entity.TbBaseDangerInvestigationReocrd;
import org.jeecg.modules.product.risk.mapper.TbBaseCheckDangerMapper;
import org.jeecg.modules.product.risk.service.ITbBaseCheckDangerService;
import org.jeecg.modules.product.risk.service.ITbBaseDangerInvestigationReocrdService;
import org.jeecg.modules.product.risk.service.ITbBaseDangerInvestigationService;
import org.jeecg.modules.product.risk.vo.ReportedBaseCheckDanger;
import org.jeecg.modules.product.risk.vo.TbBaseCheckDangerDto;
import org.jeecg.modules.product.riskUnit.mapper.TbBaseRiskDeviceMapper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 专项检查隐患信息
 * @Author: jeecg-boot
 * @Date:   2024-05-08
 * @Version: V1.0
 */
@Service
@Slf4j
public class TbBaseCheckDangerServiceImpl extends ServiceImpl<TbBaseCheckDangerMapper, TbBaseCheckDanger> implements ITbBaseCheckDangerService, Job {
//    @Value("${doubleControl.companyCode}")
//    private String companyCode;
//    @Value("${doubleControl.uploadCheckDangerUrl}")
//    private String uploadCheckDangerUrl;
    @Autowired
    private TbBaseCheckDangerMapper baseCheckDangerMapper;
    @Autowired
    private TbBaseRiskDeviceMapper tbBaseRiskDeviceMapper;
    @Autowired
    private ITbBaseDangerInvestigationService tbBaseDangerInvestigationService;
    @Autowired
    private ITbBaseDangerInvestigationReocrdService tbBaseDangerInvestigationReocrdService;
    private static int count = 500;

    @Override
    public boolean addRecombinationDanger(TbBaseCheckDangerDto baseCheckDangerDto) {
        /*---------------add隐患信息--------------------begin-------------------------------*/
        TbBaseDangerInvestigation baseDangerInvestigation = new TbBaseDangerInvestigation();
        baseDangerInvestigation.setId(UUIDGenerator.generate());
        baseDangerInvestigation.setDangerName(baseCheckDangerDto.getDangerName());
        baseDangerInvestigation.setDangerDesc(baseCheckDangerDto.getDangerDesc());
        baseDangerInvestigation.setRiskDeviceId(baseCheckDangerDto.getRiskDeviceId());
        baseDangerInvestigation.setRiskUnitId(baseCheckDangerDto.getRiskUnitId());
        baseDangerInvestigation.setDangerSrc(baseCheckDangerDto.getDangerSrc());
        baseDangerInvestigation.setDangerLevel(baseCheckDangerDto.getDangerLevel());
        baseDangerInvestigation.setDangerManageType(baseCheckDangerDto.getDangerManageType());
        baseDangerInvestigation.setHazardDangerType(baseCheckDangerDto.getHazardDangerType());
        baseDangerInvestigation.setDangerReason(baseCheckDangerDto.getDangerReason());
        baseDangerInvestigation.setControlMeasures(baseCheckDangerDto.getControlMeasures());
        baseDangerInvestigation.setCost(baseCheckDangerDto.getCost());
        baseDangerInvestigation.setImg(baseCheckDangerDto.getImg());
        baseDangerInvestigation.setLiablePerson(baseCheckDangerDto.getLiablePerson());
        baseDangerInvestigation.setCheckAcceptTime(baseCheckDangerDto.getCheckAcceptTime());
        baseDangerInvestigation.setDangerState(baseCheckDangerDto.getDangerState());
        baseDangerInvestigation.setDeleted(CommonConstant.DEL_FLAG_0.toString());
        baseDangerInvestigation.setHazardCategory(baseCheckDangerDto.getHazardCategory());
        baseDangerInvestigation.setRegistTime(DateUtils.getDate());
        baseDangerInvestigation.setRegistrant(baseCheckDangerDto.getRegistrant());
        tbBaseDangerInvestigationService.save(baseDangerInvestigation);
        /*---------------add隐患信息--------------------end-------------------------------*/
        /*---------------add隐患治理信息--------------------begin-------------------------------*/
        List<TbBaseDangerInvestigationReocrd> baseDangerInvestigationReocrdList = baseCheckDangerDto.getBaseDangerInvestigationReocrdList();
        if(!baseDangerInvestigationReocrdList.isEmpty()){
            for(TbBaseDangerInvestigationReocrd entity:baseDangerInvestigationReocrdList) {
                //外键设置
                entity.setId(UUIDGenerator.generate());
                entity.setInvestigationId(baseDangerInvestigation.getId());
                tbBaseDangerInvestigationReocrdService.save(entity);
            }
        }
        /*---------------add隐患治理信息-------------------end-------------------------------*/
        /*---------------add专项检查隐患信息--------------------begin-------------------------------*/
        TbBaseCheckDanger baseCheckDanger = new TbBaseCheckDanger();
        baseCheckDanger.setId(UUIDGenerator.generate());
        baseCheckDanger.setDangerId(baseDangerInvestigation.getId());
        baseCheckDanger.setTaskId(baseCheckDangerDto.getTaskId());
        baseCheckDanger.setCheckType(baseCheckDangerDto.getCheckType());
        baseCheckDanger.setCheckItemId(baseCheckDangerDto.getCheckItemId());
        baseCheckDanger.setCheckScoreId(baseCheckDangerDto.getCheckScoreId());
        baseCheckDanger.setCheckDate(DateUtils.getDate());
        baseCheckDanger.setCheckPay(baseCheckDangerDto.getCheckPay());
        baseCheckDanger.setHarmType(baseCheckDangerDto.getHarmType());
        baseCheckDanger.setCheckItemId(baseCheckDangerDto.getCheckItemId());
        baseCheckDanger.setDeleted(CommonConstant.DEL_FLAG_0.toString());
        int i = baseCheckDangerMapper.insert(baseCheckDanger);
        return i > 0 ? true : false;
    }

    @Override
    public boolean editRecombinationDanger(TbBaseCheckDangerDto baseCheckDangerDto) {
        /*---------------add隐患信息--------------------begin-------------------------------*/
        TbBaseDangerInvestigation baseDangerInvestigation = new TbBaseDangerInvestigation();
        baseDangerInvestigation.setId(baseCheckDangerDto.getId());
        baseDangerInvestigation.setDangerName(baseCheckDangerDto.getDangerName());
        baseDangerInvestigation.setRiskDeviceId(baseCheckDangerDto.getRiskDeviceId());
        baseDangerInvestigation.setRiskUnitId(baseCheckDangerDto.getRiskUnitId());
        baseDangerInvestigation.setDangerSrc(baseCheckDangerDto.getDangerSrc());
        baseDangerInvestigation.setDangerLevel(baseCheckDangerDto.getDangerLevel());
        baseDangerInvestigation.setDangerManageType(baseCheckDangerDto.getDangerManageType());
        baseDangerInvestigation.setHazardDangerType(baseCheckDangerDto.getHazardDangerType());
        baseDangerInvestigation.setDangerReason(baseCheckDangerDto.getDangerReason());
        baseDangerInvestigation.setControlMeasures(baseCheckDangerDto.getControlMeasures());
        baseDangerInvestigation.setCost(baseCheckDangerDto.getCost());
        baseDangerInvestigation.setImg(baseCheckDangerDto.getImg());
        baseDangerInvestigation.setLiablePerson(baseCheckDangerDto.getLiablePerson());
        baseDangerInvestigation.setCheckAcceptTime(baseCheckDangerDto.getCheckAcceptTime());
        baseDangerInvestigation.setDangerState(baseCheckDangerDto.getDangerState());
        baseDangerInvestigation.setDeleted(baseCheckDangerDto.getDeleted());
        baseDangerInvestigation.setHazardCategory(baseCheckDangerDto.getHazardCategory());
        baseDangerInvestigation.setRegistTime(baseCheckDangerDto.getRegistTime());
        tbBaseDangerInvestigationService.updateById(baseDangerInvestigation);
        /*---------------add隐患信息--------------------end-------------------------------*/
        /*---------------add隐患治理信息--------------------begin-------------------------------*/
//        List<TbBaseDangerInvestigationReocrd> baseDangerInvestigationReocrdList = baseCheckDangerDto.getBaseDangerInvestigationReocrdList();
//        if(!baseDangerInvestigationReocrdList.isEmpty()){
//            for(TbBaseDangerInvestigationReocrd entity:baseDangerInvestigationReocrdList) {
//                //外键设置
//                entity.setId(UUIDGenerator.generate());
//                entity.setInvestigationId(baseDangerInvestigation.getId());
//                tbBaseDangerInvestigationReocrdService.save(entity);
//            }
//        }
        /*---------------add隐患治理信息-------------------end-------------------------------*/
        /*---------------add专项检查隐患信息--------------------begin-------------------------------*/
        TbBaseCheckDanger baseCheckDanger = new TbBaseCheckDanger();
        baseCheckDanger.setId(baseCheckDangerDto.getBaseCheckDangerId());
        baseCheckDanger.setDangerId(baseDangerInvestigation.getId());
        baseCheckDanger.setTaskId(baseCheckDangerDto.getTaskId());
        baseCheckDanger.setCheckType(baseCheckDangerDto.getCheckType());
        baseCheckDanger.setCheckItemId(baseCheckDangerDto.getCheckItemId());
        baseCheckDanger.setCheckScoreId(baseCheckDangerDto.getCheckScoreId());
        baseCheckDanger.setCheckDate(baseCheckDangerDto.getCheckDate());
        baseCheckDanger.setCheckPay(baseCheckDangerDto.getCheckPay());
        baseCheckDanger.setHarmType(baseCheckDangerDto.getHarmType());
        baseCheckDanger.setDeleted(baseCheckDangerDto.getDeleted());
        int i = baseCheckDangerMapper.updateById(baseCheckDanger);
        return i > 0 ? true : false;
    }

    @Override
    public TbBaseCheckDangerDto queryRecombinationDangerByDangerId(String id) {
        return baseCheckDangerMapper.queryRecombinationDangerByDangerId(id);
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("-----------专项排查任务数据开始同步双重预防系统---------------------------------------------");
        try {
            List<ReportedBaseCheckDanger> list = assembleReportedVo();
            log.info(list.size()+"--------------------查询到的排查记录数量为：---------------------"+list);
            if(!list.isEmpty()){
                //printList(list);
            }
        }catch (Exception e){
            log.error("专项排查任务数据上传应急部异常--------------"+e);
        }
    }

    private List<ReportedBaseCheckDanger> assembleReportedVo() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        Date date =new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        /*在当前时间上减去天数*/
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        Date firstDays = calendar.getTime();
        List<ReportedBaseCheckDanger> list = new ArrayList<>();
        List<TbBaseCheckDanger> recordList = baseCheckDangerMapper.selectList(new QueryWrapper<>(new TbBaseCheckDanger()).between("check_date",sdf.format(firstDays),sdf.format(date)));
        if (!recordList.isEmpty()) {
            for (TbBaseCheckDanger entity : recordList) {
                ReportedBaseCheckDanger reported = new ReportedBaseCheckDanger();
                reported.setId(entity.getId());
                //reported.setCompanyCode(companyCode);
                reported.setDangerId(entity.getDangerId());
                reported.setTaskId(entity.getTaskId());
                reported.setCheckType(entity.getCheckType());
                reported.setCheckItemId(entity.getCheckItemId());
                reported.setCheckScoreId(entity.getCheckScoreId());
                reported.setCheckDate(sdf2.format(entity.getCheckDate()));
                reported.setCheckPay(entity.getCheckPay());
                reported.setHarmType(entity.getHarmType());
                reported.setDeleted(entity.getDeleted());
                reported.setCreateBy(entity.getCreateBy());
                reported.setCreateDate(sdf.format(entity.getCreateTime()));
                reported.setUpdateBy("admin");
                reported.setUpdateDate(sdf.format(entity.getCreateTime()));
                list.add(reported);
            }
        }
        return list;
    }

//    private void printList(List<ReportedBaseCheckDanger> list) {
//        List<ReportedBaseCheckDanger> logList = new ArrayList<>();
//        if (!list.isEmpty()){
//            for (int i = 0; i < list.size();i++) {
//                logList.add(list.get(i));
//                if (logList.size() == count || i == list.size() - 1){
//                    HashMap<String,String> map = uploadingDoubleControlUtil.updateReporteData(JSON.toJSONString(logList),uploadCheckDangerUrl);
//
//                    addLog(map);
//                    logList.clear();
//                }
//            }
//        }
//    }

//    public void addLog(HashMap<String,String> resData){
//        JSONObject resultJson = JSON.parseObject(resData.get("result"));
//        String code = resultJson.getString("code");
//        String msg = resultJson.getString("msg");
//        String obj = resultJson.getString("obj");
//        FeedUploadBatchLog log = new FeedUploadBatchLog();
//        log.setStatus(code);
//        log.setErrorContent(msg);
//        log.setBatch(obj);
//        log.setType("8");
//        log.setAuthorizationHeader(resData.get("authorization"));
//        feedUploadBatchLogMapper.insert(log);
//    }
}
